﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.Admin.Web.Models;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class CartController : Controller
    {
        public ICartService CartService { get; set; }
        public IUserService UserService { get; set; }
        public ICartRecordService CartRecordService { get; set; }
        // GET: Cart
        [CheckPermission("Cart.List")]
        public ActionResult List(int pageIndex = 1)
        {
            CartListModel model = new CartListModel();
            LuShanTealPager page = new LuShanTealPager();
            model.CartRecords = CartRecordService.GetPage(pageIndex, 10);
            page.CurrentLinkClassName = "";
            page.PageIndex = pageIndex;
            page.TotalCount = CartRecordService.GetTotalCount();
            page.UrlPattern = "/AdminUser/List?pageIndex={pn}";
            model.Page = page.GetPager();
            return View(model);
        }
        [HttpPost]
        [CheckPermission("Cart.Delete")]
        public ActionResult Delete(long id)
        {
            CartRecordService.MarkDelete(id);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        [CheckPermission("Cart.GetByUserId")]
        public ActionResult GetByUserId(long userId)
        {
            var user = UserService.GetById(userId);
            if (user == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "该用户不存在" });
            }
            long cartId = CartService.GetCartIdByUserId(userId);
            var model = CartRecordService.GetNoBuyCartByCartId(cartId);
            return Json(new AjaxResult { Status = "ok", Data = model });
        }

    }
}